package uml.view.factory;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.TextField;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.plaf.TextUI;
import javax.swing.text.Caret;

import net.miginfocom.swing.MigLayout;

import uml.objects.UMLObject;
import uml.util.UMLConstants;
import uml.util.UMLTextArea;
import uml.util.listeners.DragListener;
import uml.view.ClassFormatter_View;
import uml.view.UMLContainerView;
import uml.view.panels.ClassView;
import uml.view.panels.InterfaceView;
import uml.view.panels.PackageView;

/**
 * A factory used to create UML objects to be placed in a View. Users only need 
 * to handle UMLContainerViews; the factory will decide how to create the specified 
 * object the user requests.  
 * 
 * @author Neil Obetz
 *
 */

public final class ContainerFactory
{

	/**
	 * Returns a default interface view UML container object and returns it to the user.
	 * @return the interface view
	 */
	public static UMLContainerView createInterfaceView ()
	{
		UMLContainerView interfaceView = new UMLContainerView ();
		interfaceView.setBackground (UMLConstants.INTERFACE_COLOR);
		interfaceView.addDragListener (new DragListener (interfaceView));
		interfaceView.setLayout (new MigLayout ("wrap 1, fillx", "1[align center]1", "0.5[]0.5"));;
		interfaceView.setBorder (UMLConstants.INTERFACE_BORDER);
		
		JLabel name = new JLabel ("Interface");
		name.setBackground (interfaceView.getBackground ());
		
		interfaceView.add (name, "wrap");
		interfaceView.add (new JTextArea ("Edit operations"), "growx, growy");
		
		return (interfaceView);
	}

	/**
	 * Returns a default class view UML container object and returns it to the user.
	 * @return the class view
	 */
	
	public static UMLContainerView createClassView ()
	{
		UMLContainerView classView = new UMLContainerView ();
		classView.addDragListener (new DragListener (classView));
		classView.setBackground (UMLConstants.CLASS_COLOR);
		classView.setBorder (UMLConstants.NORMAL);
		classView.setLayout (new MigLayout ("wrap 1, fillx", "1[align center]1", "0.5[]0.5"));
		
		JLabel name = new JLabel ("Class");
		name.setBackground (classView.getBackground ());
		name.setMinimumSize (new Dimension (20, 20));

		classView.add (name, "wrap");
		
		JTextArea attributes = new JTextArea ("Edit attributes");
		JTextArea operations = new JTextArea ("Edit operations");
		JTextArea responsibilities = new JTextArea ("Edit responsibilities");
		
		attributes.setBackground (UMLConstants.COMPONENT_BACKGROUND);
		operations.setBackground (UMLConstants.COMPONENT_BACKGROUND);
		responsibilities.setBackground (UMLConstants.COMPONENT_BACKGROUND);
		
		classView.add (attributes, "growx, growy, wrap");
		classView.add (operations, "growx, , growy, wrap");
		classView.add (responsibilities, "growx, growy");
		
		return (classView);
	}
	
	/**
	 * Creates a default note view UML container and returns it to the user.
	 * @return the note view
	 */
	
	public static UMLContainerView createNoteView ()
	{
		UMLContainerView note = new UMLContainerView ();
		note.setLayout (new MigLayout ("filly", "[align center]"));
		note.setBackground (UMLConstants.NOTE_BACKGROUND);
		note.setSize (UMLConstants.NOTE_SIZE);
		note.setBorder (UMLConstants.NOTE_BORDER);
		note.addDragListener (new DragListener (note));
		
		JLabel name = new JLabel ("Note");
		
		JTextArea noteArea = new JTextArea ("Edit comment");
		noteArea.setBackground (UMLConstants.COMPONENT_BACKGROUND);
		
		note.add (name, "wrap");
		note.add (new JSeparator (), "growx, wrap");
		note.add (noteArea, "growx");
		
		ClassFormatter_View formatter = new ClassFormatter_View ();
		formatter.setContainer (note);
		formatter.setFontItalic ();
		
		return (note);
	}
	
	/**
	 * Creates a default alternate circle style UML interface.
	 * @return the circle style interface.
	 */
	
	public static UMLContainerView createCircleInterfaceView ()
	{
		UMLContainerView circleInterface = new UMLContainerView ();
		circleInterface.setLayout (new MigLayout ("filly", "[align center]"));
		circleInterface.setBackground (UMLConstants.NOTE_BACKGROUND);
		circleInterface.setSize (UMLConstants.NOTE_SIZE);
		circleInterface.addDragListener (new DragListener (circleInterface));
		
		JLabel circleLabel = new JLabel ();
		circleLabel.setBackground (Color.pink);
		
		circleLabel.setBackground (circleInterface.getBackground ());
		BufferedImage iconImage = new BufferedImage(25,25,BufferedImage.TYPE_INT_RGB);
		Graphics2D circle = iconImage.createGraphics();
		
		circle.setColor (Color.WHITE);
		circle.fillRect (0, 0, iconImage.getWidth (), iconImage.getHeight ());
		circle.setColor (Color.BLACK);
		circle.drawOval ((iconImage.getWidth ()-20)/2, (iconImage.getHeight ()-20)/2, 20, 20);
		circleLabel.print(circle);
		ImageIcon circleIcon = new ImageIcon(iconImage);
		circleLabel.setIcon (circleIcon);
		
		JTextArea operations = new JTextArea ("Edit operations");
		operations.setBackground (UMLConstants.COMPONENT_BACKGROUND);
		
		circleInterface.add (circleLabel, "wrap");
		circleInterface.add (new JSeparator (), "growx, wrap");
		circleInterface.add (operations, "growx");
		
		ClassFormatter_View formatter = new ClassFormatter_View ();
		formatter.setContainer (circleInterface);
		formatter.setFontItalic ();
		
		return (circleInterface);
	}
	
	/**
	 * Creates a package view UML container object and returns it to the user.
	 * @return the package view
	 */
	
	public static UMLContainerView createPackageView ()
	{
		Border sharedBorder = UMLConstants.PACKAGE_BORDER;
		
		UMLContainerView tab = new UMLContainerView ();
		tab.setBackground (UMLConstants.PACKAGE_COLOR);
		tab.setLayout (new MigLayout ("", "[align center]"));
		tab.add (new JLabel ("Package"));
		tab.setBorder (sharedBorder);
		
		UMLContainerView body = new UMLContainerView ();
		body.setBackground (UMLConstants.PACKAGE_COLOR);
		body.setBorder (sharedBorder);
		
		UMLContainerView packageView = new UMLContainerView ();
		
		packageView.setLayout (new MigLayout ("wrap 1, fillx", "[align Left]", "[]0"));
		packageView.add (tab, "wrap, height 20, width 80");
		packageView.add (body, "height 200, width 400, growy, growy");
		
		packageView.setOpaque (false);
		packageView.addDragListener (new DragListener (packageView));
		
		return (packageView);
	}
}
